<template>
	<div class="userBg">
		<div class="searchBox">
			<u-search @search="sList = [];search(0)" @custom="sList = [];search(0)" :clearabled="true" placeholder="请输入中班老师" v-model="searchVal" :actionStyle="{color:'#fff'}"></u-search>
		</div>
		<div class="container">
			<div class="userList" v-for="xbls in xblsList" :key="xbls">
				<u-divider :text="xbls" textPosition="left" :textSize="26"></u-divider>
				<ul>
					<li v-for="(item,index) in filterXbls(xbls)" :key="index" :userId="item._id" @click="changePath(item)">
						<u-button :plain="true" type="error" :text="item.name"></u-button>
					</li>
				</ul>
			</div>
			
		</div>
		<div class="loadBox" v-if="searchLoading">
			<u-loading-icon size="36" mode="circle" :vertical="true" color="red" text="加载中..." textSize="20"></u-loading-icon>
		</div>
		
		<canvas canvas-id="waterMark" class="waterMark" style="width: 100vw; height: 100vh;"></canvas>
		<image :src="waterMarkUrl" class="waterMark-bg" style="width: 100vw; height: 100vh;"></image>
	</div>
</template>

<script>
	import {
		mapActions
	} from 'vuex';
	const db = wx.cloud.database()
	export default {
		data() {
			return {
				banner: 'https://www.baixuemianshi.com/zhuanti/tiku/images/banner.jpg?' + new Date(),
				waterMarkUrl: "",
				windowWidth: 100,
				windowHeight: 100,
				zbls:"",
				sList:[],
				cpList:[],
				searchLoading:false,
				xblsList:[],
				searchVal:"",
				moniList:[],
				
				subs:['学员模式','期数模式'],
				curIndex:0,
				l:[]
			}
		},
		onShow() {
			if(wx.setVisualEffectOnCapture){
			    wx.setVisualEffectOnCapture({
			        visualEffect: 'hidden',
			        success:(res) => {
			            console.log('success回调',res)
			        },
			    })
			}
		},
		onHide() {
			if(wx.setVisualEffectOnCapture){
			    wx.setVisualEffectOnCapture({
			        visualEffect: 'none',
			        success:(res) => {
			            console.log('success回调',res)
			        },
			    })
			}
		},
		onLoad(options) {
			
		},
		computed: {
			userphone() {
				return this.$store.getters.userphone ? this.$store.getters.userphone : '';
			}
			
		},
		created() {
			let that = this;
		},
		mounted() {
			let that = this 
			if(this.userphone){
				this.drawWaterMark('雪恒教育-白雪面试 ' + this.userphone);
			}else{
				this.drawWaterMark('雪恒教育-白雪面试');
			}
		},
		watch: {
			
		},
		methods: {
			...mapActions(['LogSetPhone','LogSetOpenid']),
			search(page){
				let that = this
				that.searchLoading = true
				db.collection('24sk_bjxx').where({
					zbls:that.searchVal
				}).skip(page * 20).limit(20).get({
					success: res =>{
						if(res.data.length > 0){
							that.sList = that.sList.concat(res.data)
							let p = page + 1
							that.search(p)
						}else{
							console.log(that.sList)
							that.filterBanji()
						}
					}
				})
			},
			changePath(info){
				let that = this
				uni.navigateTo({
					url:'./student?userid=' + info._id
				})
			},
			filterBanji(){
				let that = this
				let xblss = that.sList.map(item => item.xbls)
				xblss = [...new Set(xblss)]
				that.xblsList = xblss
				that.searchLoading = false
			},
			filterXbls(name){
				let list = this.sList.filter(item => item.xbls == name)
				return list
			},
			
			filterQs(){
				let that = this
				wx.cloud.callFunction({
					name: 'aggregate',
					data: {
						fnc:'24sk_pingjia',
						field: '$moni'
					},
					success: res => {
						console.log(res)
						that.moniList = that.moniList.concat(res.result.list[0].categories)
					},
					fail: req => {
						console.log(req)
					}
				});
			},
			changeMoni(moni){
				let that = this
				uni.navigateTo({
					url:'./student2?moni=' + moni
				})
			},
			sectionChange(e){
				this.curIndex = e
			},
			
			drawWaterMark(text) {
				this.getDeviceInfo().then(response => {
					const w = response.windowWidth;
					const h = response.windowHeight;
					this.windowWidth = w;
					this.windowHeight = h;
					//创建canvas绘制上下文
					const canvas = uni.createCanvasContext("waterMark", this);
					canvas.clearRect(0, 0, w, h)
					//开始创建一个路径，需要调用fill或者stroke才会使用路径进行填充或描边。
					canvas.beginPath();
					canvas.setFontSize(18);
					canvas.setFillStyle('rgba(0, 0, 0, 0.1)');
					canvas.rotate(-30 * Math.PI / 180)
					if (text) {
						text = this.trim(text)
					}
					const maxWidth = text.length * 18
					for (let x = 0; x < 10; x++) {
						for (let y = 0; y < 20; y++) {
							//在画布上绘制被填充的文本。
							canvas.fillText(text, (maxWidth + 40) * x - 400, 90 * y + 20);
						}
					}
					//将之前在绘图上下文中的描述（路径、变形、样式）画到 canvas 中。
					canvas.draw(false, () => {
						//把当前画布指定区域的内容导出生成指定大小的图片
						uni.canvasToTempFilePath({
							canvasId: "waterMark",
							complete: (res) => {
								this.waterMarkUrl = res.tempFilePath
							}
						}, this)
					})
				});
			},
			trim(str) {
				let trimStr = '';
				const reg = /[\t\r\f\n\s]*/g;
				if (typeof str === 'string') {
					trimStr = str.replace(reg, '');
				}
				return trimStr
			},
			/**
			 * 获取当前设备的信息
			 * **/
			getDeviceInfo() {
				return new Promise((resolve, reject) => {
					uni.getSystemInfo({
						success: (sysInfo) => {
							resolve(sysInfo)
						}
					})
				})
			}
		},
		components: {
			
		}
	};
</script>

<style scoped>
	@import url(./index.css);
</style>